from rest_framework import serializers
from .models import *
from .handles import get_kubeinfo_from_yaml

class KubernetesSerializer(serializers.ModelSerializer):
    server = serializers.SerializerMethodField()
    user = serializers.SerializerMethodField()
    class Meta:
        model = Kubernetes
        fields = '__all__'
        extra_kwargs = {
            'kubeconfig': {'write_only': True}
        }

    def get_server(self, obj):
        server = get_kubeinfo_from_yaml(obj.kubeconfig).get('server')
        return server

    def get_user(self, obj):
        user = get_kubeinfo_from_yaml(obj.kubeconfig).get('user')
        return user


class DockerRepoSerializer(serializers.ModelSerializer):
    class Meta:
        model = DockerRepo
        fields = '__all__'
        # write_only_fields = ['password', 'access_key_secret']
        extra_kwargs = {
            'password': {'write_only': True},
            'access_key_secret': {'write_only': True}
        }

class BusinessSerializer(serializers.ModelSerializer):
    class Meta:
        model = Business
        fields = '__all__'



class ProjectFSelectSerializer(serializers.ModelSerializer):
    value = serializers.SerializerMethodField(source='get_value')
    label = serializers.SerializerMethodField(source='get_label')

    class Meta:
        model = Project
        fields = ['value', 'label']

    def get_value(self, obj):
        return obj.id

    def get_label(self, obj):
        return obj.name

class BusinessProjectSelectSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField(source='get_children')
    value = serializers.SerializerMethodField(source='get_value')
    label = serializers.SerializerMethodField(source='get_label')
    class Meta:
        model = Business
        fields = [ 'value', 'label', 'children']
    def get_value(self, obj):
        return 'bus-'+str(obj.id)

    def get_label(self, obj):
        return obj.name
    
    def get_children(self, obj):
        children = obj.projects.all()
        serializer = ProjectFSelectSerializer(children, many=True)
        if serializer.data == []:
            return None
        return serializer.data
    



class ProjectSerializer(serializers.ModelSerializer):
    business_name = serializers.SerializerMethodField(read_only=True)

    def get_business_name(self, obj):
        return obj.business.name
    class Meta:
        model = Project
        fields = '__all__'



class ServiceSerializer(serializers.ModelSerializer):
    # business = serializers.SerializerMethodField(source='get_business')
    business_name = serializers.SerializerMethodField(read_only=True)
    project_git_url = serializers.SerializerMethodField(read_only=True)
    project_git_master_branch = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = Service
        fields = '__all__'
    # def get_business(self, obj):
    #     return obj.project.business.id

    def get_business_name(self, obj):
        return obj.business.name
    def get_project_git_url(self, obj):
        return obj.project.git_url

    def get_project_git_master_branch(self, obj):
        return obj.project.git_master_branch

class BusinessSelectSerializer(BusinessSerializer):
    class Meta:
        model = Business
        fields = ['id', 'name', 'code']

class ProjectSelectSerializer(ProjectSerializer):
    class Meta:
        model = Project
        fields = ['id', 'name', 'code']

class ServiceSelectSerializer(ServiceSerializer):
    class Meta:
        model = Service
        fields = ['id', 'name', 'code']


class LobPermSerializer(serializers.ModelSerializer):
    user_name = serializers.SerializerMethodField(source='user.name')
    role_names = serializers.SerializerMethodField()
    class Meta:
        model = LobPerm
        fields = ['id', 'user', 'user_name', 'business', 'roles', 'role_names']


    def get_user_name(self, obj):
        return obj.user.name

    def get_role_names(self, obj):
        data = []
        for role in obj.roles.all():
            data.append(role.name)
        return data

class EnvironmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Environment
        fields = '__all__'